package com.boot.hospital.score.core.controller;


import com.boot.hospital.score.common.enums.ResultCodeEnum;
import com.boot.hospital.score.common.wrapper.ResponseWrapper;
import com.boot.hospital.score.core.service.CadreService;
import com.boot.hospital.score.dao.entity.Cadre;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cadres")
@Slf4j
@PropertySources(value = {
        @PropertySource(value = {"classpath:important.properties", "classpath:application.properties"}, encoding = "utf-8")
})
@EnableAutoConfiguration
public class CadreController {

    @Resource
    private CadreService cadreService;

    @ApiOperation("添加参评人员信息")
    @PostMapping(value = "/addCadres")
    public ResponseWrapper<Object> addCadre(@RequestParam Map<String, String> params) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {

            Cadre cadre = new Cadre();
            cadre.setCadresNumber(params.get("cadresNumber"));
            cadre.setCadresName(params.get("cadresName"));
            cadre.setDept(params.get("dept"));
            cadre.setPosition(params.get("position"));
            cadre.setOrders(Integer.valueOf(params.get("orders")));
            cadre.setReviewId(Integer.valueOf(params.get("reviewId")));
            cadre.setIntroduction(params.get("introduction"));
            cadre.setGender(params.get("gender"));
            cadre.setBirth(params.get("birth"));
            cadre.setNation(params.get("nation"));
            cadre.setParty(params.get("party"));
            cadre.setJoinTime(params.get("joinTime"));
            cadre.setTitle(params.get("title"));
            cadre.setDutyTime(params.get("dutyTime"));
            cadre.setGetTime(params.get("getTime"));
            cadre.setPublishTime(params.get("publishTime"));
            cadre.setTeachingTitle(params.get("teachingTitle"));
            cadre.setDuties(params.get("duties"));
            cadre.setEducation(params.get("education"));
            cadre.setDegree(params.get("degree"));
            cadre.setGraduation(params.get("graduation"));
            cadre.setMajor(params.get("major"));
            cadre.setGraduateTime(params.get("graduateTime"));
            cadre.setNativePlace(params.get("nativePlace"));
            cadre.setDescription(params.get("description"));
            cadre.setMemo(params.get("memo"));

            if (cadre.getCadresNumber().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员工号为空");
                return responseWrapper;
            }
            if (cadre.getPosition().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员职称为空");
                return responseWrapper;
            }
            if (cadre.getCadresName().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员姓名为空");
                return responseWrapper;
            }
            if (cadre.getOrders().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员序号为空");
                return responseWrapper;
            }
            if (cadre.getDept().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员部门为空");
                return responseWrapper;
            }

            int result = cadreService.insert(cadre);
            if (result > 0) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.ADD_FAIL.getName());
            }
        } catch (Exception e) {
            log.error("添加参评人员信息失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }
//    @ApiOperation("查询所有参评人员信息")
//    @GetMapping(value = "/selectAll")
//    public ResponseWrapper<Object> selectAll() {
//        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
//        try {
//            List<Cadre> cadres = cadreService.selectAll();
//            responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
//            responseWrapper.setData(cadres);
//        } catch (Exception e) {
//            log.error("查询所有参评人员失败：{}", ExceptionUtils.getStackTrace(e));
//            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
//            responseWrapper.setCode("1");
//        }
//        return responseWrapper;
//    }

    @ApiOperation("根据IDs删除参评人员记录")
    @DeleteMapping(value = "/deleteCadres")
    public ResponseWrapper<Object> deleteCadresByIds(@RequestBody List<Integer> cadreIds) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            if (cadreIds.size() <= 0) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("id为空");
                return responseWrapper;
            }
            int result = cadreService.deleteCadresByIds(cadreIds);
            if (result > 0) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("数据库删除失败");
            }
        } catch (Exception e) {
            log.error("根据IDs删除参评人员记录失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }


    @ApiOperation("更新参评人员记录")
    @PostMapping(value = "/updateCadres")
    public ResponseWrapper<Object> updateReview(@RequestParam Map<String, String> params) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {

            Cadre cadre = new Cadre();
            cadre.setCadresNumber(params.get("cadresNumber"));
            cadre.setCadresName(params.get("cadresName"));
            cadre.setDept(params.get("dept"));
            cadre.setPosition(params.get("position"));
            cadre.setOrders(Integer.valueOf(params.get("orders")));
            cadre.setReviewId(Integer.valueOf(params.get("reviewId")));
            cadre.setIntroduction(params.get("introduction"));
            cadre.setGender(params.get("gender"));
            cadre.setBirth(params.get("birth"));
            cadre.setNation(params.get("nation"));
            cadre.setParty(params.get("party"));
            cadre.setJoinTime(params.get("joinTime"));
            cadre.setTitle(params.get("title"));
            cadre.setDutyTime(params.get("dutyTime"));
            cadre.setGetTime(params.get("getTime"));
            cadre.setPublishTime(params.get("publishTime"));
            cadre.setTeachingTitle(params.get("teachingTitle"));
            cadre.setDuties(params.get("duties"));
            cadre.setEducation(params.get("education"));
            cadre.setDegree(params.get("degree"));
            cadre.setGraduation(params.get("graduation"));
            cadre.setMajor(params.get("major"));
            cadre.setGraduateTime(params.get("graduateTime"));
            cadre.setNativePlace(params.get("nativePlace"));
            cadre.setDescription(params.get("description"));
            cadre.setMemo(params.get("memo"));
            cadre.setCadresId(Integer.valueOf(params.get("id")));

            if (cadre.getCadresNumber().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员工号为空");
                return responseWrapper;
            }
            if (cadre.getPosition().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员职称为空");
                return responseWrapper;
            }
            if (cadre.getCadresName().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员姓名为空");
                return responseWrapper;
            }
            if (cadre.getOrders().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员序号为空");
                return responseWrapper;
            }
            if (cadre.getDept().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("参评人员部门为空");
                return responseWrapper;
            }
            int result = cadreService.updateCadre(cadre);
            if (result > 0) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.UPDATE_FAIL.getName());
            }
        } catch (Exception e) {
            log.error("更新参评人员记录失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }

    @ApiOperation("查询参评人员基本信息")
    @GetMapping(value = "/getCadresBasic/{page}/{size}")
    public ResponseWrapper<Object> getCadresBasic(@PathVariable Integer page, @PathVariable Integer size, @RequestParam String search, @RequestParam Integer reviewId) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            Map<String, Object> params = new HashMap<>();
            if (search.equals("null") || search.equals("NULL")) {
                params.put("search", "");
            }
            params.put("pageSize", size);
            params.put("offset", page);
            params.put("search", search);
            params.put("reviewId", reviewId);

            List<Cadre> cadres = cadreService.selectByPageAndSearchAndName(params);
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
            responseWrapper.setData(cadres);
        } catch (Exception e) {
            log.error("查询参评人员基本信息：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }

    @ApiOperation("查询参评人员信息")
    @GetMapping(value = "/select/{cadreId}")
    public ResponseWrapper<Object> selectByCadreId(@PathVariable Integer cadreId) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            Cadre cadre = cadreService.selectByCadreId(cadreId);
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
            responseWrapper.setData(cadre);
        } catch (Exception e) {
            log.error("查询参评人员失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }

    @ApiOperation("根据评审id查询参评人员详细信息")
    @GetMapping(value = "/getCadresDetailByReviewId")
    public ResponseWrapper<Object> getCadresDetailByReviewId(@RequestParam Integer reviewId) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            Cadre cadre = cadreService.selectByReviewId(reviewId);
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
            responseWrapper.setData(cadre);
        } catch (Exception e) {
            log.error("根据评审id查询参评人员详细信息：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }

    @ApiOperation("根据参评人员Id查询详细信息")
    @GetMapping(value = "/getCadresDetailByCadresId")
    public ResponseWrapper<Object> getCadresDetailByCadresId(@RequestParam Integer cadresId) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            Cadre cadre = cadreService.getCadresDetailByCadresId(cadresId);
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
            responseWrapper.setData(cadre);
        } catch (Exception e) {
            log.error("根据参评人员Id查询详细信息：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }
}